<template>
  <div>
    <div class="mb">
      <el-input
        placeholder="请输入姓名"
        v-model="studentName"
        style="width: 200px; margin-right: 10px"
      ></el-input>
      <el-button type="primary" @click="serch()">查询</el-button>
      <el-button icon="el-icon-plus" @click="add()" type="primary"></el-button>
    </div>
    <el-table :data="data" border>
      <el-table-column label="学生" prop="studentName"></el-table-column>
      <el-table-column label="学科" prop="type"></el-table-column>
      <el-table-column label="分数" prop="score"></el-table-column>

      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button type="success" @click="bj(scope.row)">编辑</el-button>

          <el-button type="dange" @click="sc(scope.row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="pageNum"
      :page-sizes="[100, 200, 300, 400]"
      :page-size="pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
    >
    </el-pagination>
    <el-dialog
      :title="title"
      :visible.sync="dialogVisible"
      width="30%"
      :before-close="handleClose"
    >
      <el-form :model="form">
        <el-form-item label="学生">
          <el-select
            v-model="form.studentNo"
            @change="xhxs"
            style="width: 100%"
          >
            <el-option
              v-for="item in data1"
              :label="item.name"
              :value="item.studentNo"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="学科">
          <el-input
            v-model="form.type"
            :readonly="title == '编辑' ? true : false"
          ></el-input>
        </el-form-item>
        <el-form-item label="分数">
          <el-input v-model="form.score"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="qx()">取 消</el-button>
        <el-button type="primary" @click="submit()">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
import service from "@/request";

export default {
  data() {
    return {
      title: "新增",
      data: [],
      dialogVisible: false,
      form: {
        studentName: "",
      },
      name: "",
      total: 0,
      pageNum: 1,
      pageSize: 10,
      studentName: "",
      data1: [],
      data2: [],
    };
  },
  mounted() {
    this.getdata();
    this.getdata1();
    this.getdata2();
  },
  methods: {
    xhxs(e) {
      this.data1.forEach((el) => {
        console.log(el, e);
        if (el.studentNo == e) {
          this.form.studentName = el.name;
          console.log(this.form.studentName);
        }
      });
    },
    sc(id) {
      this.$confirm("此操作将永久删除该文件, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(async () => {
          const res = await service.get("/score/delete", {
            params: {
              id: id,
            },
          });
          if (res.code == 200) {
            this.$message({
              type: "success",
              message: "删除成功!",
            });
            this.getdata();
                        this.getdata2();
          }
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    add() {
      this.title = "新增";
      this.dialogVisible = true;
      this.form = {};
    },
    bj(row) {
      this.title = "编辑";
      this.form = JSON.parse(JSON.stringify(row));
      this.dialogVisible = true;
    },

    handleClose() {
      this.dialogVisible = false;
    },
    qx() {
      this.form = {};
      this.dialogVisible = false;
    },
    async submit() {
      if (this.title == "新增") {
        let sfxs = false;
        // this.data2.forEach((el) => {
        //   console.log(el.type, this.form.type, this.form.studentNo);
        //   if (el.studentNo == this.form.studentNo) {
        //     if (el.type === this.form.type) {
        //       sfxs = true;
        //       return;
        //     } else {
        //       sfxs = false;
        //     }
        //   }
        // });
        for (var i = 0; i < this.data2.length; i++) {
              console.log()
          if (this.data2[i].studentNo == this.form.studentNo) {
            if (this.data2[i].type == this.form.type) {
              sfxs = true;
               break;
            } else {
              sfxs = false;
            }
          }
        }
        if (sfxs) {
          this.$message.warning("该学生当前学科已有成绩，不可重复添加");
        } else {
          const res = await service.post("/score/add", this.form);
          console.log(res);
          if (res.code == 200) {
            this.dialogVisible = false;
            this.$message.success(res.msg);
            this.getdata();
            this.getdata2();
          }
        }
      } else {
        const res = await service.post("/score/update", this.form);
        console.log(res);
        if (res.code == 200) {
          this.dialogVisible = false;
          this.form = {};
          this.$message.success(res.msg);
          this.getdata();
          this.getdata2();
        }
      }
    },
    handleCurrentChange(e) {
      this.pageNum = e;
      this.getdata();
    },
    handleSizeChange(e) {
      this.pageSize = e;
      this.getdata();
    },
    serch() {
      this.pageNum = 1;
      this.pageSize = 10;
      this.getdata();
    },
    async getdata() {
      const res = await service.get("/score/index", {
        params: {
          studentName: this.studentName,
          pageNum: this.pageNum,
          pageSize: this.pageSize,
        },
      });
      this.data = res.data;
      this.total = res.total;
    },
    async getdata1() {
      const res = await service.get("/student/all", {});
      this.data1 = res.data;
    },

    async getdata2() {
      const res = await service.get("/score/all", {});
      this.data2 = res.data;
    },
  },
};
</script>
<style scoped>
.mb {
  margin-bottom: 10px;
}
</style>